/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author phihai
 */
public class NoiDung_DAO {
    Database db = new Database();
    Connection conn = null;
    PreparedStatement pst = null;
    
    
    public ArrayList<NoiDung_DTO> LayNoiDung(int IDTopic) throws Exception
    {  
        ArrayList<NoiDung_DTO> arr = new ArrayList<NoiDung_DTO>(); 
        String sql = "SELECT * FROM NoiDung WHERE IDChuyenMuc="+IDTopic+" ORDER BY IDNoiDung DESC;";      
        conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        
        ResultSet rs = pst.executeQuery();
        while (rs.next())
        {           
            NoiDung_DTO nd = new NoiDung_DTO();
            nd.setId(rs.getInt("IDNoiDung"));           
            nd.setTen(rs.getString("TenTitTle"));
            nd.setThoiGian(rs.getString("ThoiGian"));
            nd.setHinhanh(rs.getBytes("HinhAnh"));
            nd.setNoiDung(rs.getString("NoiDung"));
            arr.add(nd);
        }     
        return arr;
    }
    
    public int XoaNoiDung(int id_noidung) throws Exception
    {
         String sql = "DELETE NoiDung WHERE IDNoiDung=?";
         conn = db.GetConn();
         pst = conn.prepareStatement(sql);
         
         pst.setInt(1, id_noidung);
         
         int kq = pst.executeUpdate();
                 
         return kq;
    }
    
    public void Themtin(int  IDchuyenmuc, String link, String title, byte[] img, String time, String content) throws SQLException
    {
        String sql = "insert into NoiDung(IDChuyenMuc,link,TenTitTle,HinhAnh,ThoiGian,NoiDung) values(?,?,?,?,?,?)"; 
        conn = db.GetConn();
        pst = conn.prepareStatement(sql);
        pst.setInt(1, IDchuyenmuc);
        pst.setString(2, link);
        pst.setString(3, title);
        pst.setBytes(4, img);
        pst.setString(5, time);
        pst.setString(6, content);
        pst.executeUpdate();
        
        
    }
    
}
